package com.ymatou.iapi.csim.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.OpenSessionBean;
import com.ymatou.iapi.csim.parameter.SendScoreMessageBean;
import com.ymatou.iapi.csim.service.OpenSessionCall;
import com.ymatou.iapi.csim.service.SendScoreMessageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
/**
*会话结束，客服发起评分
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendScoreMessage{
	private static SendScoreMessageBean bean;
	private static SendScoreMessageCall call;
	private CsimCallService service=new CsimCallService(); 
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服发送请求评分消息-会话结束，客服发起评分");
	}

	@Before
	public void caseUp() {
		bean=new SendScoreMessageBean();
		call=new SendScoreMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessage_1() {
		Logger.start(true,"会话结束,客服发起评分");
		try {
    		//查询是否有在线客服 1，2
    		service.getOnlineCS();
    		//打开会话
    		ymt_cssession csSession=service.getOpenSession(csId,userId);
    		
    		bean.setCsId(csId);
    		bean.setUserId(userId);
    		bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("0",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals("",call.getResult().get("ErrorMessage"), "验证返回的errorMessage");
			Logger.verifyEquals(true,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals("",call.getMsg().toString(), "验证返回的Msg");
			Logger.verifyEquals("200",call.getStatus(), "验证返回的status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessage_2() {
		Logger.start(true,"会话结束,客服发起评分-会话关闭");
		try {
			//查询是否有在线客服 1，2
    		service.getOnlineCS();
    		//打开会话
    		ymt_cssession csSession=service.getOpenSession(csId,userId);
    		
    		csSession.setStatus(1);
    		csSessionWapper.updateByPrimaryKey(csSession);
    		
    		bean.setCsId(csId);
    		bean.setUserId(userId);
    		bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("会话已关闭"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("会话已关闭"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendScoreMessage_3() {
		Logger.start(true,"会话结束,客服发起评分-会话Id不存在");
		try {
			//查询是否有在线客服 1，2
    		service.getOnlineCS();
    		bean.setCsId(csId);
    		bean.setUserId(userId);
    		bean.setSessionId("11");
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("会话不存在"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("会话不存在"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SendScoreMessage_4() {
		Logger.start(true,"会话结束,客服发起评分-客服不存在(目前没有对客服做校验)");
		try {
			//查询是否有在线客服 1，2
    		service.getOnlineCS();
    		//3:查询是否有会话
    		int count=csSessionWapper.selectBy_Count(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
    		OpenSessionCall openSessionCall=new OpenSessionCall();
    		ymt_cssession csSession=new ymt_cssession();
    		if(count==0){
				//主动开启会话
				OpenSessionBean openSessionBean=service.setOpenSessionBean();
				openSessionBean.setToUserId(userId);
				openSessionCall=service.OpenSessionCall(openSessionBean);
				csSession.setId(openSessionCall.getSeesionId());
    		}
    		else
    		{
    		    csSession=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csId,"UserId",userId,"Status",0));
    		}
    		bean.setCsId(12312);
    		bean.setUserId(userId);
    		bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("会话不存在"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getMsg().toString().contains("会话不存在"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}